﻿using System;
using System.Drawing;
using System.Net;
using System.IO;

using Newtonsoft.Json.Linq;

namespace xbmc_json_async.System
{
    public class XHelpers
    {
        public static XSettings Settings;

        public static Uri ArtworkUri(string thumbnail)
        {
           return new Uri(String.Format("http://{0}:{1}/vfs/{2}", Settings.IpAddress, Settings.Port, thumbnail)); 
        }

        public static Image GetImageFromThumbnail(String thumbnail)
        {
            WebRequest imageGetter = WebRequest.Create(ArtworkUri(thumbnail));
            imageGetter.Credentials = new NetworkCredential(Settings.UserName, Settings.Password);
            Image retreievedImage = null;

            try
            {
                WebResponse response = imageGetter.GetResponse();

                Stream stream = response.GetResponseStream();
                //var stream = imageGetter.GetResponse().GetResponseStream();
                if (stream != null)
                {
                    retreievedImage = new Bitmap(stream);
                    stream.Close();
                }
            }
            catch (WebException wex)
            {
                return null;
            }
            catch
            {
                retreievedImage = new Bitmap(32, 32);
            }

            return retreievedImage;
        }

        public static Boolean CheckResponseForError(JObject response, out XError error)
        {
            if (response["error"] != null)
            {
                error = XError.FromJson((JObject)response["error"]);
                return true;
            }
            error = null;
            return false;

        }

        /// <summary>
        /// Extracts the specified int value from a jObject
        /// must already be at the right level
        /// If the item doesnt exist, returns 0
        /// </summary>
        /// <param name="jObject">The j object.</param>
        /// <param name="itemRequired">The item required.</param>
        /// <returns></returns>
        public static Int32 JvalInt(JObject jObject, string itemRequired)
        {
            if (jObject[itemRequired] != null)
            {
                return Convert.ToInt32(jObject[itemRequired].Value<JValue>().Value.ToString()); 
            }
            else { return 0; }
            
        }

        /// <summary>
        /// Js the val double.
        /// </summary>
        /// <param name="jObject">The j object.</param>
        /// <param name="itemRequired">The item required.</param>
        /// <returns></returns>
        public static Double JValDouble(JObject jObject, string itemRequired)
        {
            if (jObject[itemRequired] != null)
            {
                return Convert.ToDouble(jObject[itemRequired].Value<JValue>().Value.ToString());
            }
            else { return 0; }

        }
        /// <summary>
        /// Gets the string value, passing a generic object instead of JObject
        /// </summary>
        /// <param name="jObject">The j object.</param>
        /// <param name="itemRequired">The item required.</param>
        /// <returns></returns>
        public static string JValGeneric(object jObject, string itemRequired)
        {
            return Jval((JObject)jObject, itemRequired);
        }
        /// <summary>
        /// Extracts the specified string value from a jObject
        /// must already be at the right level!
        /// </summary>
        /// <param name="jObject">The j object.</param>
        /// <param name="itemRequired">The item required.</param>
        /// <returns></returns>
        public static string Jval(JObject jObject, string itemRequired)
        {
            if (jObject[itemRequired] != null)
            { return jObject[itemRequired].Value<JValue>().Value.ToString(); }
            else { return string.Empty; }
        }
    }
}
